package com.control.qct.businesssteward.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.model.AccountDetailModel;
import com.control.qct.businesssteward.model.AllAccountDetailModel;
import com.control.qct.businesssteward.utils.DecimalFormatUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/12/13 0013.
 */

public class AccountDetialChildAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private List<AllAccountDetailModel.CashierSaleOrderListBean.TotalTypesBean> mData;

    public AccountDetialChildAdapter(Context context, List<AllAccountDetailModel.CashierSaleOrderListBean
            .TotalTypesBean> data) {
        this.mContext = context;
        this.mData = data;
    }

    @Override
    public int getGroupCount() {
        return mData.size();
    }


    @Override
    public Object getGroup(int groupPosition) {
        return mData.get(groupPosition);
    }


    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }


    @Override
    public boolean hasStableIds() {
        return false;
    }


    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.elv_account_detail_item, null);
            holder.mTvAccountType = convertView.findViewById(R.id.tv_account_type);
            holder.mTvAccountCount = convertView.findViewById(R.id.tv_account_count);
            holder.mTvAccountPrice = convertView.findViewById(R.id.tv_account_price);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mTvAccountType.setText(mData.get(groupPosition).getTitle());
        holder.mTvAccountCount.setText(mData.get(groupPosition).getCount() > 0 ?
                mData.get(groupPosition).getCount() + "" : "");
        holder.mTvAccountPrice.setText(DecimalFormatUtils.DecimalFormatTwo(mData.get(groupPosition).getMoney()));
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mData.get(groupPosition).getPayTypes().size();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (groupPosition >= 0) {
            return mData.get(groupPosition).getPayTypes().size();
        } else {
            return 0;
        }
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup
            viewGroup) {
        ChildViewHolder holder = null;
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = View.inflate(mContext, R.layout.elv_account_detail_child_item, null);
            holder.mTvAccountType = convertView.findViewById(R.id.tv_account_type);
            holder.mTvAccountCount = convertView.findViewById(R.id.tv_account_count);
            holder.mTvAccountPrice = convertView.findViewById(R.id.tv_account_price);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        holder.mTvAccountType.setText(mData.get(groupPosition).getPayTypes().get(childPosition)
                .getTitle());
        holder.mTvAccountCount.setText(mData.get(groupPosition).getPayTypes().get(childPosition).getCount() + "");
        holder.mTvAccountPrice.setText(DecimalFormatUtils.DecimalFormatTwo(mData.get(groupPosition).getPayTypes().get
                (childPosition).getMoney()));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class ViewHolder {
        private TextView mTvAccountType, mTvAccountCount, mTvAccountPrice;
    }

    class ChildViewHolder {
        private TextView mTvAccountType, mTvAccountCount, mTvAccountPrice;
    }
}
